package com.joven.product.service.infrastructure.converter;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joven.product.service.business.domain.model.entity.BaseDomainObj;
import com.joven.product.service.business.domain.model.entity.id.DomainObjId;
import com.joven.product.service.infrastructure.persistence.db.entity.BaseDataObj;
import com.joven.infra.common.model.PageResult;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.List;

public interface BaseConverter<T extends DomainObjId, DomainObj extends BaseDomainObj<T>, DataObj extends BaseDataObj> {
    DomainObj toDomainObj(DataObj dataObj);

    @Mapping(source = "id", target = "preId", qualifiedByName = "getEntityPreId")
    DataObj toDataObj(DomainObj domainObj);

    List<DataObj> toDataObjs(List<DomainObj> domainObjs);

    default DomainObjId toEntityId(Long id) {
        return new DomainObjId(id);
    }

    default Long getEntityIdValue(DomainObjId entityId) {
        if (entityId == null) {
            return null;
        }
        return entityId.getValue();
    }

    @Named("getEntityPreId")
    default String getEntityPreId(DomainObjId entityId) {
        if (entityId == null) {
            return null;
        }
        return entityId.getPreId();
    }

    @Mapping(source = "records", target = "records", ignore = true)
    PageResult convert(Page page);
}
